package org.grayrabbit.commons.dao;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.grayrabbit.commons.util.logging.Logger;
import org.grayrabbit.commons.util.logging.LoggerFactory;

/**
 * 查询条件
 * @author sys53
 */
public class QueryCondition extends HashMap<String, Object> {
	private static final long serialVersionUID = -6083992862177865810L;
	private static final Logger log = LoggerFactory.getLogger(QueryCondition.class);
	/**
	 * sql语句间的空格
	 */
	private static final String SQL_BLANK = " ";
	private StringBuffer queryString;
	private StringBuffer whereString;
	private Map<String,Object> paramAndValues = new HashMap<String,Object>();
    private List<Object> values = new ArrayList<Object>(0);
    private List<String> orders = new ArrayList<String>(0);
    private int offset = 0;
    private int maxResults = 0;
    public QueryCondition(){
    	queryString = new StringBuffer();
    	whereString = new StringBuffer();
    }
    public QueryCondition(String fromStr){
    	this();
    	queryString.append(fromStr);
    }
	/**
	 * 获取起始列
	 * @return
	 */
	public int getOffset() {
		return offset;
	}
	
	public QueryCondition appendQueryString(String queryString){
		if(this.queryString==null)
			this.queryString = new StringBuffer();
		this.queryString.append(queryString);
		return this;
	}

	public int getMaxResults() {
		return maxResults;
	}

	public QueryCondition setMaxResults(int maxResults) {
		this.maxResults = maxResults;
		return this;
	}

	/**
	 * 设置起始列
	 * @param offset
	 */
	public QueryCondition setOffset(int offset) {
		this.offset = offset;
		return this;
	}
	
	public QueryCondition add(String whereStr,Object value){
		whereString.append(SQL_BLANK);
		whereString.append(whereStr);
		values.add(value);
		log.debug(whereString + ":value=" + value.toString());
		return this;
	}
	
	public QueryCondition add(String whereStr,String name,Object value){
		whereString.append(SQL_BLANK);
		whereString.append(whereStr);
		paramAndValues.put(name, value);
		log.debug(whereString+":name="+name+",value="+value.toString());
		return this;
	}
	public QueryCondition append(String whereStr){
		whereString.append(SQL_BLANK);
		whereString.append(whereStr);
		log.debug("whereString:{}",whereString);
		return this;
	}
	
	public QueryCondition appenOrderBy(String orderStr){
		orders.add(orderStr);
		return this;
	}
	/**
	 * 添加set子语句，注意逗号不要过多或者缺少
	 * 
	 * @param setStr
	 * @param name
	 * @param value
	 * @return
	 */
	public QueryCondition addSet(String setStr,String name,Object value){
		queryString.append(setStr);
		this.paramAndValues.put(name, value);
		return this;
	}
	public void clear(){
		offset = 0;
		maxResults = 0;
		queryString = new StringBuffer();
		whereString = new StringBuffer();
		paramAndValues.clear();
		values.clear();
		orders.clear();
	}
	
	/**
	 * 获取查询语句
	 * @return
	 */
	public String getQueryString(){
		StringBuffer sb = new StringBuffer(queryString.toString());
		if(whereString!=null && whereString.length()>0){
			sb.append(" Where 1=1");
			sb.append(whereString);
		}
		if(orders!=null && orders.size()>0){
			sb.append(" Order By ");
			for (String orderBy : orders) {
				sb.append(orderBy);
				sb.append(",");
			}
			return sb.substring(0, sb.length()-1);
		}
		return sb.toString();
	}
	public Object getParamValue(String param) {
		if(paramAndValues==null)
			return null;
		return paramAndValues.get(param);
	}
	public List<Object> getValues() {
		return values;
	}
	
	public Set<String> getParamNames(){
		return paramAndValues.keySet();
	}
	
	public Collection<Object> getParamValues(){
		return paramAndValues.values();
	}
	
	public boolean isParamQuery(){
		if(paramAndValues!=null && paramAndValues.size()>0)
			return true;
		return false;
	}
	@Override
	public int size() {
		return paramAndValues.size();
	}
	@Override
	public boolean isEmpty() {
		return paramAndValues.isEmpty();
	}
	@Override
	public Object get(Object key) {
		return paramAndValues.get(key);
	}
	@Override
	public boolean containsKey(Object key) {
		return paramAndValues.containsKey(key);
	}
	@Override
	public Object put(String key, Object value) {
		return paramAndValues.put(key, value);
	}
	@Override
	public void putAll(Map<? extends String, ? extends Object> m) {
		paramAndValues.putAll(m);
	}
	@Override
	public Object remove(Object key) {
		return paramAndValues.remove(key);
	}
	@Override
	public boolean containsValue(Object value) {
		return paramAndValues.containsValue(value);
	}
	@Override
	public Set<String> keySet() {
		return paramAndValues.keySet();
	}
	@Override
	public Collection<Object> values() {
		return paramAndValues.values();
	}
	@Override
	public Set<java.util.Map.Entry<String, Object>> entrySet() {
		return paramAndValues.entrySet();
	}
	
	
}
